<template>
  <div class="app-container">
    <el-form :model="queryParams" :disabled="loading" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="卡口名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入场所名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查类型" prop="checkType">
        <el-select v-model="queryParams.checkType" clearable @visible-change="(v) => querySelect(v, 'checkType')" @change="queryCheckInfo">
          <el-option
            v-for="(item, index) in checkTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable>
          <el-option
            v-for="dict in dict.type.check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:fireRecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:fireRecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:fireRecord:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="!Object.keys(queryParams).length"
          type="success"
          plain
          icon="el-icon-printer"
          size="mini"
          @click="handlePrint"
          v-hasPermi="['system:fireRecord:print']"
        >打印</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="场所名称" align="center" prop="fireInfo.name" />
      <el-table-column label="检查类型" align="center" prop="checkType" />
      <el-table-column label="创建人" align="center" prop="createrName" />
      <el-table-column label="创建日期" align="center" prop="createTime" width="180" />
      <el-table-column label="操作人" align="center" prop="operterName" />
      <el-table-column label="操作时间" align="center" prop="operTime" width="180" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.check_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:record:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:record:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改九小场所检查记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="40">
          <el-col :span="14">
            <el-form-item label="卡口名称" prop="fireInfo.name">
              <el-select
                v-model="form.fireInfo.name"
                filterable
                remote
                reserve-keyword
                placeholder="请输入关键词"
                :remote-method="queryStore"
                :loading="loadingQueryStore"
                @change="changeStore"
              >
                <el-option
                  v-for="item in storeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="山系" prop="mount">
              <el-select v-model="form.fireInfo.mount" clearable>
                <el-option
                  v-for="(dict, index) in dict.type.mountain_data"
                  :key="dict.value"
                  :label="dict.label"
                  :value="index"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="村负责人" prop="fireInfo.fzr">
              <el-input v-model="form.fireInfo.fzr" placeholder="请输入村负责人" />
            </el-form-item>
            <el-form-item label="包村干部" prop="fireInfo.cadre">
              <el-input v-model="form.fireInfo.cadre" placeholder="请输入包村干部" />
            </el-form-item>
            <el-form-item label="片长" prop="fireInfo.manager">
              <el-input v-model="form.fireInfo.manager" placeholder="请输入片长" />
            </el-form-item>
            <el-form-item label="部门包卡口" prop="fireInfo.dutyDept">
              <el-input v-model="form.fireInfo.dutyDept" placeholder="请输入部门包卡口" />
            </el-form-item>
            <el-form-item label="包山领导" prop="fireInfo.leader">
              <el-input v-model="form.fireInfo.leader" placeholder="请输入包山领导" />
            </el-form-item>
            <el-form-item label="备注" prop="memo">
              <el-input v-model="form.memo" type="textarea" placeholder="请输入备注" />
            </el-form-item>
            <div v-if="getFormStatus === 1">
              <el-form-item label="检查人员签字(合格)" prop="okSign1">
                <div v-if="form.okSign1">
                  <img :src="form.okSign1" style="width: 300px;height: auto;">
                  <el-button type="primary" size="mini" @click="reSign('okSign1')">重签</el-button>
                </div>
                <div v-else>
                  <vue-esign ref="okSign1" style="width: 100%;height: 300px;border: 1px solid #ccc;" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" />
                  <el-button type="primary" size="mini" @click="confirmSign('okSign1')">提交</el-button>
                  <el-button type="danger" size="mini" @click="resetSign('okSign1')">重置</el-button>
                </div>
              </el-form-item>
              <el-form-item label="场所人员签字(合格)" prop="okSign2">
                <div v-if="form.okSign2">
                  <img :src="form.okSign2" style="width: 300px;height: auto;">
                  <el-button type="primary" size="mini" @click="reSign('okSign2')">重签</el-button>
                </div>
                <div v-else>
                  <vue-esign ref="okSign2" style="width: 100%;height: 300px;border: 1px solid #ccc;" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" />
                  <el-button type="primary" size="mini" @click="confirmSign('okSign2')">提交</el-button>
                  <el-button type="danger" size="mini" @click="resetSign('okSign2')">重置</el-button>
                </div>
              </el-form-item>
              <el-form-item label="主管部门签字(合格)" prop="okSign3">
                <div v-if="form.okSign3">
                  <img :src="form.okSign3" style="width: 300px;height: auto;">
                  <el-button type="primary" size="mini" @click="reSign('okSign3')">重签</el-button>
                </div>
                <div v-else>
                  <vue-esign ref="okSign3" style="width: 100%;height: 300px;border: 1px solid #ccc;" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" />
                  <el-button type="primary" size="mini" @click="confirmSign('okSign3')">提交</el-button>
                  <el-button type="danger" size="mini" @click="resetSign('okSign3')">重置</el-button>
                </div>
              </el-form-item>
            </div>
            <div v-if="getFormStatus === 0">
              <el-form-item label="检查人员签字(不合格)" prop="nookSign1">
                <div v-if="form.okSign3">
                  <img :src="form.okSign3" style="width: 300px;height: auto;">
                  <el-button type="primary" size="mini" @click="reSign('okSign3')">重签</el-button>
                </div>
                <div v-else>
                  <vue-esign ref="okSign3" style="width: 100%;height: 300px;border: 1px solid #ccc;" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" />
                  <el-button type="primary" size="mini" @click="confirmSign('okSign3')">提交</el-button>
                  <el-button type="danger" size="mini" @click="resetSign('okSign3')">重置</el-button>
                </div>
              </el-form-item>
              <el-form-item label="场所人员签字(不合格)" prop="nookSign2">
                <div v-if="form.nookSign2">
                  <img :src="form.nookSign2" style="width: 300px;height: auto;">
                  <el-button type="primary" size="mini" @click="reSign('nookSign2')">重签</el-button>
                </div>
                <div v-else>
                  <vue-esign ref="nookSign2" style="width: 100%;height: 300px;border: 1px solid #ccc;" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" />
                  <el-button type="primary" size="mini" @click="confirmSign('nookSign2')">提交</el-button>
                  <el-button type="danger" size="mini" @click="resetSign('nookSign2')">重置</el-button>
                </div>
              </el-form-item>
              <el-form-item label="主管部门签字(不合格)" prop="nookSign3">
                <div v-if="form.nookSign3">
                  <img :src="form.nookSign3" style="width: 300px;height: auto;">
                  <el-button type="primary" size="mini" @click="reSign('nookSign3')">重签</el-button>
                </div>
                <div v-else>
                  <vue-esign ref="nookSign3" style="width: 100%;height: 300px;border: 1px solid #ccc;" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" />
                  <el-button type="primary" size="mini" @click="confirmSign('nookSign3')">提交</el-button>
                  <el-button type="danger" size="mini" @click="resetSign('nookSign3')">重置</el-button>
                </div>
              </el-form-item>
            </div>
            <div v-if="getFormStatus === 2">
              <el-form-item label="检查人员签字(整改)" prop="todoSign1">
                <div v-if="form.todoSign1">
                  <img :src="form.todoSign1" style="width: 300px;height: auto;">
                  <el-button type="primary" size="mini" @click="reSign('todoSign1')">重签</el-button>
                </div>
                <div v-else>
                  <vue-esign ref="todoSign1" style="width: 100%;height: 300px;border: 1px solid #ccc;" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" />
                  <el-button type="primary" size="mini" @click="confirmSign('todoSign1')">提交</el-button>
                  <el-button type="danger" size="mini" @click="resetSign('todoSign1')">重置</el-button>
                </div>
              </el-form-item>
              <el-form-item label="场所人员签字(整改)" prop="todoSign2">
                <div v-if="form.todoSign2">
                  <img :src="form.todoSign2" style="width: 300px;height: auto;">
                  <el-button type="primary" size="mini" @click="reSign('todoSign2')">重签</el-button>
                </div>
                <div v-else>
                  <vue-esign ref="todoSign2" style="width: 100%;height: 300px;border: 1px solid #ccc;" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" />
                  <el-button type="primary" size="mini" @click="confirmSign('todoSign2')">提交</el-button>
                  <el-button type="danger" size="mini" @click="resetSign('todoSign2')">重置</el-button>
                </div>
              </el-form-item>
              <el-form-item label="主管部门签字(整改)" prop="todoSign3">
                <div v-if="form.todoSign3">
                  <img :src="form.todoSign3" style="width: 300px;height: auto;">
                  <el-button type="primary" size="mini" @click="reSign('todoSign3')">重签</el-button>
                </div>
                <div v-else>
                  <vue-esign ref="todoSign3" style="width: 100%;height: 300px;border: 1px solid #ccc;" :isCrop="isCrop" :lineWidth="lineWidth" :lineColor="lineColor" :bgColor.sync="bgColor" />
                  <el-button type="primary" size="mini" @click="confirmSign('todoSign3')">提交</el-button>
                  <el-button type="danger" size="mini" @click="resetSign('todoSign3')">重置</el-button>
                </div>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="10">
            <el-form-item label="检查类型" prop="checkType">
              <el-select v-model="form.checkType" clearable @visible-change="(v) => querySelect(v, 'checkType')" @change="queryCheckInfo">
                <el-option
                  v-for="(item, index) in checkTypes"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="显示合格" prop="showOk">
              <el-switch
                v-model="form.showOk"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
            <div v-if="form.dtlList.length">
              <div v-for="(item, index) in form.dtlList" :key="index">
                <div v-show="item.status !== 1 || form.showOk" style="padding: 10px 0;border-top: 1px solid #ccc;">
                  <el-form-item label="检查内容" prop="checkinfoid">
                    <el-select v-model="item.checkinfoid" clearable>
                      <el-option
                        v-for="(info, index) in checkInfos"
                        :key="info.value"
                        :label="info.label"
                        :value="info.value"
                      />
                    </el-select>
                  </el-form-item>
                  <el-form-item label="检查照片" prop="checkPhotos">
                    <el-upload
                      :action="uploadUrl"
                      :file-list="item.checkPhotos"
                      list-type="picture-card"
                      class="upload"
                      :on-success="(res, file, fileList) => uploadImgSuccess(res, file, fileList, 'checkPhotos', item)"
                      :before-upload="file => uploadBefore(file, 'checkPhotos')"
                      :before-remove="delPhoto"
                    >
                      <i class="el-icon-plus"></i>
                    </el-upload>
                  </el-form-item>
                  <el-form-item label="是否合格" prop="isOk">
                    <el-switch
                      v-model="item.isOk"
                      :disabled="!!form.id"
                      active-color="#13ce66"
                      inactive-color="#ff4949"
                      :active-value="1"
                      :inactive-value="0">
                    </el-switch>
                  </el-form-item>
                  <el-form-item v-if="item.isOk === 0" label="整改建议" prop="content">
                    <el-input v-model="item.content" disabled placeholder="请输入整改建议" />
                  </el-form-item>
                  <el-form-item v-if="item.isOk === 0 && item.recordId" label="整改照片" prop="rectifyPhotos">
                    <el-upload
                      :action="uploadUrl"
                      :file-list="item.rectifyPhotos"
                      list-type="picture-card"
                      class="upload"
                      :on-success="(res, file, fileList) => uploadImgSuccess(res, file, fileList, 'rectifyPhotos', item)"
                      :before-upload="file => uploadBefore(file, 'rectifyPhotos')"
                      :before-remove="delPhoto"
                    >
                      <i class="el-icon-plus"></i>
                    </el-upload>
                  </el-form-item>
                  <el-form-item v-if="item.isOk === 0 && item.recordId" label="是否合格" prop="status">
                    <el-switch
                      v-model="item.status"
                      active-color="#13ce66"
                      inactive-color="#ff4949"
                      :active-value="1"
                      :inactive-value="0">
                    </el-switch>
                  </el-form-item>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改九小场所检查记录对话框 -->
    <el-dialog :title="title" :visible.sync="openDetail" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="40">
          <el-col :span="10">
            <el-form-item label="卡口名称" prop="fireInfo.name">
              {{ form.fireInfo.name }}
            </el-form-item>
            <el-form-item label="检查类型" prop="checktypeid">
              {{ form.typename }}
            </el-form-item>
            <el-form-item label="检查日期" prop="checkdate">
              {{ form.checkdate }}
            </el-form-item>
            <el-form-item label="创建人" prop="creater">
              {{ form.createrName }}
            </el-form-item>
            <el-form-item v-if="form.operterName" label="操作人" prop="operter">
              {{ form.operterName }}
            </el-form-item>
            <el-form-item v-if="form.operTime" label="操作时间" prop="operTime">
              {{ form.operTime }}
            </el-form-item>
            <el-form-item v-if="form.okSign1 && form.status === 1" label="检查人员签字（合格）" prop="okSign1">
              <img :src="form.okSign1" style="width: 300px;height: auto;">
            </el-form-item>
            <el-form-item v-if="form.okSign2 && form.status === 1" label="场所人员签字(合格)" prop="okSign2">
              <img :src="form.okSign2" style="width: 300px;height: auto;">
            </el-form-item>
            <el-form-item v-if="form.okSign3 && form.status === 1" label="主管部门签字(合格)" prop="okSign3">
              <img :src="form.okSign3" style="width: 300px;height: auto;">
            </el-form-item>
            <el-form-item v-if="form.nookSign1 && form.status === 2" label="检查人员签字(不合格)" prop="nookSign1">
              <img :src="form.nookSign1" style="width: 300px;height: auto;">
            </el-form-item>
            <el-form-item v-if="form.nookSign2 && form.status === 2" label="场所人员签字(不合格)" prop="nookSign2">
              <img :src="form.nookSign2" style="width: 300px;height: auto;">
            </el-form-item>
            <el-form-item v-if="form.nookSign3 && form.status === 2" label="主管部门签字(不合格)" prop="nookSign3">
              <img :src="form.nookSign3" style="width: 300px;height: auto;">
            </el-form-item>
            <el-form-item v-if="form.todoSign1 && form.status === 0" label="检查人员签字(整改)" prop="todoSign1">
              <img :src="form.todoSign1" style="width: 300px;height: auto;">
            </el-form-item>
            <el-form-item v-if="form.todoSign2 && form.status === 0" label="场所人员签字(整改)" prop="todoSign2">
              <img :src="form.todoSign2" style="width: 300px;height: auto;">
            </el-form-item>
            <el-form-item v-if="form.todoSign3 && form.status === 0" label="主管部门签字(整改)" prop="todoSign3">
              <img :src="form.todoSign3" style="width: 300px;height: auto;">
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <div v-for="(item, index) in form.dtlList" :key="item.id" style="padding: 10px 0; border-bottom: 1px solid #bbb;">
              <el-form-item label="检查内容">
                <dict-tag :options="dict.type.check_status" :value="item.status" style="display: inline-block;"/> {{ item.checkinfo }}
              </el-form-item>
              <el-form-item v-if="item.checkPhotos && item.checkPhotos.length" label="检查照片">
                <span v-for="(img, i) in item.checkPhotos" :key="i">
                  <el-image
                    style="width: 100px; height: 100px"
                    :src="img.url"
                    :preview-src-list="item.checkPhotos.map(p => p.url)">
                  </el-image>
                </span>
              </el-form-item>
              <el-form-item v-if="!!item.content && item.isOk === 0" label="整改建议" prop="content">
                {{ item.content }}
              </el-form-item>
              <el-form-item v-if="item.rectifyPhotos && item.rectifyPhotos.length" label="整改照片">
                <span v-for="(img, i) in item.rectifyPhotos" :key="i">
                  <el-image
                    style="width: 100px; height: 100px"
                    :src="img.url"
                    :preview-src-list="item.rectifyPhotos.map(p => p.url)">
                  </el-image>
                </span>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="openDetail = false">确 定</el-button>
        <el-button @click="openDetail = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/system/fireRecord";
import { getdeptByparentid } from '@/api/system/dept'
import { listFireChecktype } from '@/api/system/fireChecktype'
import { listFireCheckInfo } from '@/api/system/fireCheckInfo'
import { listFireInfo } from '@/api/system/fireInfo'
import {uploadUrl, uploadFile} from "@/api/system/config";
import { base64ToImg, formatDate1 } from '@/utils'
import vueEsign from 'vue-esign'
import { parseTime } from '@/utils/ruoyi'
import store from "@/store";
import {getUserList} from "@/api/system/user";

export default {
  name: "Record",
  dicts: ['sys_normal_disable', 'check_status', 'mountain_data', 'store_status'],
  components: {
    vueEsign
  },
  data() {
    return {
      uploadUrl,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 九小场所检查记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      openShare: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      date: [],
      selection: [],
      // 表单参数
      form: {
        fireInfo: {
          name: ''
        },
        dtlList: [],
        showOk: 1
      },
      formShare: {},
      // 表单校验
      rules: {
      },
      rulesShare: {},
      props: {
        checkStrictly: true,
        lazy: true,
        lazyLoad (node, resolve) {
          const params = {
            parentid: ['admin', 'adm'].filter(item => store.getters.roles.includes(item)).length ? 200 : store.getters.deptId,
            deptType: '0'
          }
          console.log('node', node)
          params.parentid = node.value || (['admin', 'adm'].filter(item => store.getters.roles.includes(item)).length ? 200 : store.getters.deptId)
          const { level } = node;
          getdeptByparentid(params).then(res => {
            const nodes = res.data.map(item => {
              return {
                value: item.deptId,
                label: item.deptName,
                leaf: level >= 1
              }
            })
            resolve(nodes)
          })
        }
      },
      storeList: [],
      storesData: [],
      loadingQueryStore: false,
      checkTypes: [],
      checkInfos: [],
      lineWidth: 6,
      lineColor: '#000000',
      bgColor: '',
      resultImg: '',
      isCrop: false,
      getFormStatus: 1,
      exportBtnTxt: '导出全部数据',
      exportBtnType: 'danger',
      users: []
    };
  },
  created() {
    this.getList();
    console.log('dept', this.deptId, this.deptName, this.roles)
  },
  computed: {
    ...mapGetters([
      'deptId',
      'deptName',
      'roles'
    ])
  },
  watch: {
    'form.dtlList': {
      handler: function(nv, ov) {
        console.log(nv, ov)
        if (nv.some(item => item.status === 0)) {
          this.getFormStatus = 0
          console.log('status', this.getFormStatus)
          return
        }
        if (nv.some(item => !item.recordId && item.isOk === 0)) {
          this.getFormStatus = 2
          console.log('status', this.getFormStatus)
          return
        }
        this.getFormStatus = 1
        console.log('status', this.getFormStatus)
      },
      deep: true
    },
    'date': {
      handler: function(nv, ov) {
        console.log('date nv ov', nv, ov)
        this.queryParams.beginDate = formatDate1(nv[0])
        this.queryParams.endDate = formatDate1(nv[1])
      }
    }
  },
  methods: {
    /** 查询九小场所检查记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    querySelect(v, type) {
      console.log('type', v, type)
      if (!v) return
      switch (type) {
        case 'checkType':
          listFireChecktype().then(res => {
            console.log('ressssss', res)
            this.checkTypes = res.rows.map(item => {
              return {
                value: item.id,
                label: item.typename
              }
            })
          })
          break
        default :
      }
    },
    queryCheckInfo(v) {
      console.log('vv', v)
      const params = {
        typeId: v,
        pageNum: 1,
        pageSize: 1000
      }
      listFireCheckInfo(params).then(res => {
        this.checkInfos = res.rows.map(item => {
          return {
            value: item.id,
            label: item.content
          }
        })
        this.form.dtlList = res.rows.map(item => {
          return {
            checkinfoid: item.id,
            checkPhotos: [],
            isOk: 1,
            content: item.zgjy,
            rectifyPhotos: [],
            status: 1
          }
        })
        console.log('infos', this.checkInfos, this.form)
      })
    },
    uploadBefore(file, type) {
      console.log('file', file)
      const isJPG = ['image/jpeg', 'image/png'].includes(file.type);
      const isLt2M = file.size / 1024 / 1024 < 5;

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG,PNG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 5MB!');
      }
      return isJPG && isLt2M;
    },
    uploadImgSuccess(res, file, fileList, type, item) {
      console.log('res', res, file, fileList)
      let typeTmp
      switch (type) {
        case 'checkPhotos':
          typeTmp = 3
          break
        case 'rectifyPhotos':
          typeTmp = 4
          break
        default:
      }
      item[type] = [
        ...item[type],
        ...[
          {
            type: typeTmp,
            url: res.fileName,
            imgUrl: res.fileName
          }
        ]
      ]
    },
    confirmSign(type) {
      this.$refs[type].generate().then(res => {
        console.log('reeeeeeee', res)
        const file = base64ToImg(res, type)
        const formData = new FormData()
        formData.append('file', file)
        uploadFile(formData).then(res => {
          console.log('res', res)
          // this.form[type] = res.fileName
          this.$set(this.form, type, res.fileName)
        })
      }).catch(err => {
        alert(err) // 画布没有签字时会执行这里 'Not Signned'
      })
    },
    resetSign(type) {
      this.$refs[type].reset()
    },
    reSign(type) {
      this.form[type] = ''
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fireInfo: {},
        dtlList: [],
        showOk: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
      this.selection = selection
      if (selection.length) {
        this.exportBtnTxt = '导出所选数据'
        this.exportBtnType = 'warning'
      } else {
        this.exportBtnTxt = '导出全部数据'
        this.exportBtnType = 'danger'
      }
    },
    queryStore(v) {
      console.log('vvv', v)
      const params = {
        name: v,
        pageNum: 1,
        pageSize: 10000
      }
      this.loadingQueryStore = true
      listFireInfo(params).then(res => {
        this.storesData = res.rows
        this.storeList = res.rows.map(item => {
          return {
            value: item.id,
            label: `${item.name} - ${item.fzr}`
          }
        })
        this.loadingQueryStore = false
      })
    },
    changeStore(v) {
      console.log('vvvv', v, this.storesData)
      const arr = this.storesData.map(item => item.id)
      const index = arr.indexOf(v)
      console.log('vvvv', v, this.storeList, arr, index)
      this.form.fireInfo.id = this.storesData[index].id
      this.form.fireInfo.mount = this.storesData[index].mount
      this.form.fireInfo.mountName = this.dict.type.mountain_data[v].text
      this.form.fireInfo.lon = this.storesData[index].lon
      this.form.fireInfo.lat = this.storesData[index].lat
      this.form.fireInfo.fzr = this.storesData[index].fzr
      this.form.fireInfo.cadre = this.storesData[index].cadre
      this.form.fireInfo.manager = this.storesData[index].manager
      this.form.fireInfo.dutyDept = this.storesData[index].dutyDept
      this.form.fireInfo.leader = this.storesData[index].leader
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      console.log('form', this.form)
      this.open = true;
      this.title = "添加防火检查记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const params = {
        typeId: row.checktypeid,
        pageNum: 1,
        pageSize: 1000
      }
      listFireCheckInfo(params).then(res => {
        this.checkInfos = res.rows.map(item => {
          return {
            value: item.id,
            label: item.content
          }
        })
        const params1 = {
          id: row.id
        }
        getRecord(params1).then(response => {
          this.form = response.data;
          for(const record of this.form.dtlList) {
            if (record.photoList.length) {
              record.checkPhotos = []
              record.rectifyPhotos = []
              for(const img of record.photoList) {
                if (img.type === 3) {
                  record.checkPhotos = [
                    ...record.checkPhotos,
                    ...[
                      {
                        id: img.id,
                        name: img.id,
                        url: img.imgUrl
                      }
                    ]
                  ]
                }
                if (img.type === 4) {
                  record.rectifyPhotos = [
                    ...record.rectifyPhotos,
                    ...[
                      {
                        id: img.id,
                        name: img.id,
                        url: img.imgUrl
                      }
                    ]
                  ]
                }
              }
            }
          }
          this.open = true;
          this.title = "修改九小场所检查记录";
        });
      })
    },
    handleDetail(row) {
      const params1 = {
        id: row.id
      }
      getRecord(params1).then(response => {
        this.form = response.data;
        for(const record of this.form.dtlList) {
          if (record.photoList.length) {
            record.checkPhotos = []
            record.rectifyPhotos = []
            for(const img of record.photoList) {
              if (img.type === 3) {
                record.checkPhotos = [
                  ...record.checkPhotos,
                  ...[
                    {
                      id: img.id,
                      name: img.id,
                      url: img.imgUrl
                    }
                  ]
                ]
              }
              if (img.type === 4) {
                record.rectifyPhotos = [
                  ...record.rectifyPhotos,
                  ...[
                    {
                      id: img.id,
                      name: img.id,
                      url: img.imgUrl
                    }
                  ]
                ]
              }
            }
          }
        }
        this.openDetail = true
        this.title = '查看记录'
      })
    },
    /** 提交按钮 */
    submitForm() {
      console.log('form', this.form)
      for (let i = 0; i < this.form.dtlList.length; i++) {
        this.form.dtlList[i].photoList = []
        this.form.dtlList[i].photoList = [
          ...this.form.dtlList[i].checkPhotos || [],
          ...this.form.dtlList[i].rectifyPhotos || []
        ]
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除九小场所检查记录编号为"' + ids + '"的数据项？').then(function() {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    delPhoto (file, fileList) {
      console.log('file', file)
      if (!file.id) return
      const params = {
        id: file.id
      }
      // delphoto(params)
    },
    /** 导出按钮操作 */
    handleExport() {
      console.log('this.queryParams', this.queryParams)
      this.download('/system/record/export', {
        ...this.queryData
      }, `record_${new Date().getTime()}.xlsx`)
    },
    handlePrint() {
      console.log('this.queryData', this.queryData)
      this.$router.push({
        path: '/fireInfo/print-data',
        query: this.queryData
      })
    },
    async queryMember() {
      console.log('this.formShare.deptId', this.formShare.deptId)
      const params = {
        deptId: this.formShare.deptId,
      }
      await getUserList(params).then(res => {
        this.users = res.data.map(item => {
          return {
            value: item.user_id,
            label: item.nick_name
          }
        })
      })
    },
    async changeDept(v) {
      console.log('v', v)
      this.formShare.deptId = v[v.length - 1]
      await this.queryMember()
    }
  }
};
</script>
<style>
.upload .el-upload--picture-card{width: 60px;height: 60px;line-height: 58px;}
.upload .el-upload-list--picture-card .el-upload-list__item{width: 60px;height: 60px;line-height: 58px;}
</style>
